package ocbn.remotelab.client;

import java.util.Vector;
import ocbn.remotelab.events.Event;

/**
 * The class that acts as a source of packet event.
 * Listeners can be added by addListener() and removed by removeListener(). 
 * <br> The packetReceived() method in each listener will be called when a Event is fired.
 * @author c8yanche
 */
public abstract class EventSource {

    protected Vector listeners = new Vector();
    /**
     * Add a listener.
     * @param listener
     */
    public void addListener(EventListener listener) {
        this.listeners.addElement(listener);
    }
    /**
     * Remove a listener.
     * @param listener
     */
    public void removeListener(EventListener listener) {
        this.listeners.removeElement(listener);
    }

    /**
     * Fire an Event out to all listeners.
     * @param e an Event to be fired to all listeners.
     */
    public void fireEvent(Event e) {
        for (int i = 0; i != this.listeners.size(); i++) {
            ((EventListener) listeners.elementAt(i)).EventReceived(e);
        }
    }
}
